package com.ziyuan.service.impl;
import cn.hutool.*;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import com.ziyuan.mapper.Usermapper;
import com.ziyuan.pojo.User;
import com.ziyuan.utils.Md5Util;
import com.ziyuan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PatchMapping;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class Userservice implements com.ziyuan.service.Userservice {
//    查询用户名是否重复
    @Autowired
    private Usermapper usermapper;
    @Override
    public User FindByUserName(String username) {
        User user= usermapper.Find(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        usermapper.register(username,md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        usermapper.update(user);
    }

    @Override
    public void image(String avatarUrl) {
//        没有返回map的threadlocal使用
//        JWT o = ThreadLocalUtil.get();
//        JWTPayload payload = o.getPayload();
//        Object id = payload.getClaim("id");
//        int i = NumberUtil.parseInt(id.toString());
        Map<String,String> map = ThreadLocalUtil.get();
//        Integer i = Integer.parseInt(map.get("id"));
        Integer i = Integer.parseInt(map.get("username"));
        usermapper.image(avatarUrl,i);
    }

    @Override
    public void updatepwd(String newPwd, String username) {
        usermapper.updatePwd(newPwd,username);
    }
}
